package com.sidecar.service.log.service.impl;

import java.util.Map;
import java.util.Objects;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.data.mongodb.core.ReactiveMongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import com.sidecar.common.dto.search.Pagination;
import com.sidecar.common.dto.search.SearchRequest;
import com.sidecar.common.dto.vo.log.request.OperationLogReq;
import com.sidecar.common.dto.vo.log.response.OperationLogVO;
import com.sidecar.service.log.entity.OperationLog;
import com.sidecar.service.log.repository.OperationLogRepository;
import com.sidecar.service.log.service.OperationLogService;

import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

@Service
public class OperationLogServiceImpl implements OperationLogService {

    @Resource
    OperationLogRepository operationLogRepository;

    @Resource
    ReactiveMongoTemplate reactiveMongoTemplate;

    @Override
    public void create(OperationLogReq req) {
        OperationLog log = new OperationLog();
        BeanUtils.copyProperties(req, log);
        operationLogRepository.insert(log);
    }

    @Override
    public void removeById(Long id) {
        operationLogRepository.deleteById(id);
    }

    @Override
    public Pagination<OperationLogVO> search(SearchRequest request) {
        Map<String, Object> paramMap = request.getConds();
        Query q = new Query();
        q.skip(request.calculateSkip());
        q.limit(request.getPageSize());
        Object ot = paramMap.get("operatorType");
        Object ou = paramMap.get("operatorUuid");
        if(Objects.nonNull(ot)) {
            q.addCriteria(Criteria.where("operatorType").is(ot));
        }
        if(Objects.nonNull(ou)) {
            q.addCriteria(Criteria.where("operatorUuid").is(ou));
        }
        Flux<OperationLog> ols = reactiveMongoTemplate.find(q, OperationLog.class);
        Mono<Long> count = reactiveMongoTemplate.count(q, OperationLog.class);
//        Pagination<OperationLog> p = new Pagination<OperationLog>(req.getPageNo(), req.getPageSize(), , ols.collectList().block());
        return null;
    }

}
